package cn.hackcloud.designpattern.proxy.cglib;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class ProxyFactory implements MethodInterceptor {
	private Object target;

	public ProxyFactory(Object target) {
		super();
		this.target = target;
	}

	public Object getProxyInstance() {
		// 创建1个工具类
		Enhancer enhancer = new Enhancer();
		// 设置父类
		enhancer.setSuperclass(target.getClass());
		//设置回调函数
		enhancer.setCallback(this);
		//创建子类对象，即代理对象
		return enhancer.create();

	}

	@Override
	public Object intercept(Object arg0, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
		Object object = method.invoke(target, args);
		return object;
	}
}
